var util = require( '../utils/util.js' );
var api = require( './api.js' );

var app = getApp();

/**
 * 网络请求方法
 * @param url {string} 请求url
 * @param data {object} 参数
 * @param successCallback {function} 成功回调函数
 * @param errorCallback {function} 失败回调函数
 * @param completeCallback {function} 完成回调函数
 * @returns {void}
 */
function requestData( url, data, successCallback, errorCallback, completeCallback ) {
    if( app.debug ) {
        console.log( 'requestData url: ', url );
    }
    wx.request( {
        url: url,
        data: data,
        header: { 'Content-Type': 'application/json' },
        success: function( res ) {
            if( app.debug ) {
                console.log( 'response data: ', res );
            }
            if( res.statusCode == 200 )
                util.isFunction( successCallback ) && successCallback( res.data );
            else
                util.isFunction( errorCallback ) && errorCallback();
        },
        error: function() {
            util.isFunction( errorCallback ) && errorCallback();
        },
        complete: function() {
            util.isFunction( completeCallback ) && completeCallback();
        }
    });
}

function requestPostData(url, data, successCallback, errorCallback, completeCallback) {
  if (app.debug) {
    console.log('requestData url: ', url);
  }
  wx.request({
    method: 'POST',
    url: url,
    data: data,
    header: { "Content-Type": "application/x-www-form-urlencoded" },
    success: function (res) {
      if (app.debug) {
        console.log('response data: ', res);
      }
      if (res.statusCode == 200)
        util.isFunction(successCallback) && successCallback(res.data);
      else
        util.isFunction(errorCallback) && errorCallback();
    },
    error: function () {
      util.isFunction(errorCallback) && errorCallback();
    },
    complete: function () {
      util.isFunction(completeCallback) && completeCallback();
    }
  });
}

//首页
function getListIndex( data,successCallback, errorCallback, completeCallback ) {
  requestData(api.getIndexList(), data, successCallback, errorCallback, completeCallback );
}
//分类页
function postListclassify(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postclassifyList(), data, successCallback, errorCallback, completeCallback);
}
//详情页
function getShowDetail(data,successCallback, errorCallback, completeCallback) {
  requestData(api.getDetailShow(), data, successCallback, errorCallback, completeCallback);
}

// 收藏
function postScDetail(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postDetailSc(), data, successCallback, errorCallback, completeCallback);
}
//取消收藏
function postQxDetail(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postDetailQx(), data, successCallback, errorCallback, completeCallback);
}
//加入购物车
function postAddflow(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postFlowadd(), data, successCallback, errorCallback, completeCallback);
}
//登录
function postLoginIm(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postImLogin(), data, successCallback, errorCallback, completeCallback);
}
//图片验证码
function postDxyz(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postyzDx(), data, successCallback, errorCallback, completeCallback);
}
//图片验证码
function postDoreg(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postregDo(), data, successCallback, errorCallback, completeCallback);
}
//图片验证码
function postListflow(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postflowList(), data, successCallback, errorCallback, completeCallback);
}
//改变状态
function postChangecheck(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postcheckChange(), data, successCallback, errorCallback, completeCallback);
}
//改变数量
function postChangenums(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postnumsChange(), data, successCallback, errorCallback, completeCallback);
}
//删除购物车
function postDelcart(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postcartDel(), data, successCallback, errorCallback, completeCallback);
}
//购物车确认订单
function postCartorder(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postorderCart(), data, successCallback, errorCallback, completeCallback);
} 
//单品订单确认
function postDetalisconfirm(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postconfirmDetalis(), data, successCallback, errorCallback, completeCallback);
}
//个人中心
function postCenterindex(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postindexCenter(), data, successCallback, errorCallback, completeCallback);
}
//我的收藏
function postCollectlist(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postlistCollect(), data, successCallback, errorCallback, completeCallback);
}
//删除收藏
function postColletdel(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postdelCollet(), data, successCallback, errorCallback, completeCallback);
}
//收货地址列表
function postListaddress(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postaddressList(), data, successCallback, errorCallback, completeCallback);
}
//添加收货地址
function postAddaddr(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postaddrAdd(), data, successCallback, errorCallback, completeCallback);
}
//收货地址修改
function postAddrupdate(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postupdateAddr(), data, successCallback, errorCallback, completeCallback);
}
//站长申请
function postVipadd(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postaddVip(), data, successCallback, errorCallback, completeCallback);
}  
//设置
function postListset(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postsetList(), data, successCallback, errorCallback, completeCallback);
}
//修改设置
function postUpdateset(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postsetUpdate(), data, successCallback, errorCallback, completeCallback);
} 
//订单列表
function postOrderlist(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postlistOrder(), data, successCallback, errorCallback, completeCallback);
} 
//发货提醒
function postToremind(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postremindTo(), data, successCallback, errorCallback, completeCallback);
} 
//查看物流
function postLogisticsto(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.posttoLogistics(), data, successCallback, errorCallback, completeCallback);
} 
//删除订单
function postDelorder(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postorderDel(), data, successCallback, errorCallback, completeCallback);
} 
//订单确认
function postOrderconfirm(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postconfirmOrder(), data, successCallback, errorCallback, completeCallback);
} 
//订单详情
function postOrderdetail(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postdetailOrder(), data, successCallback, errorCallback, completeCallback);
} 
//优惠券
function postTovoucher(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postvoucherTo(), data, successCallback, errorCallback, completeCallback);
} 
//领取优惠券列表
function postGetvoucher(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postvoucherGet(), data, successCallback, errorCallback, completeCallback);
} 
//领取优惠券
function postToreceive(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postreceiveTo(), data, successCallback, errorCallback, completeCallback);
} 
//积分兑换列表
function postTofract(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postfractTo(), data, successCallback, errorCallback, completeCallback);
} 
//积分兑换详情
function postFractshow(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postshowFract(), data, successCallback, errorCallback, completeCallback);
} 
//积分明细
function postFractdetail(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postdetailFract(), data, successCallback, errorCallback, completeCallback);
} 
//兑换记录
function postFractrecord(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postrecordFract(), data, successCallback, errorCallback, completeCallback);
} 
//兑换记录详情
function postFractfinish(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postfinishFract(), data, successCallback, errorCallback, completeCallback);
} 
//积分检测
function postCheckscore(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postscoreCheck(), data, successCallback, errorCallback, completeCallback);
} 
//积分检测
function postFractconfirm(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postconfirmFract(), data, successCallback, errorCallback, completeCallback);
} 
//地址变更
function postAddrmoren(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postmorenAddr(), data, successCallback, errorCallback, completeCallback);
} 
//地址删除
function postAddrdel(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postdelAddr(), data, successCallback, errorCallback, completeCallback);
} 
//微信支付
function postPaywx(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postwxPay(), data, successCallback, errorCallback, completeCallback);
} 
//优惠券使用
function postUsevoucher(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postvoucherUse(), data, successCallback, errorCallback, completeCallback);
} 
//重新认证
function postVipagain(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postagainVip(), data, successCallback, errorCallback, completeCallback);
} 
//二维码
function postVipqrcode(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postqrcodeVip(), data, successCallback, errorCallback, completeCallback);
} 
//生成订单
function postAddorder(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postorderAdd(), data, successCallback, errorCallback, completeCallback);
} 
//退款
function postOrderrefund(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postrefundOrder(), data, successCallback, errorCallback, completeCallback);
} 
//积分订单生成
function postFractcreate(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postcreateFract(), data, successCallback, errorCallback, completeCallback);
}
//评价
function postEvalist(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postlistEva(), data, successCallback, errorCallback, completeCallback);
}
//发表评价
function postEvaadd(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postaddEva(), data, successCallback, errorCallback, completeCallback);
}
//评价操作
function postDoadd(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postaddDo(), data, successCallback, errorCallback, completeCallback);
}
//公告详情
function postnoticedetail(data, successCallback, errorCallback, completeCallback) {
  requestPostData(api.postdetailnotice(), data, successCallback, errorCallback, completeCallback);
}
module.exports = {
  getListIndex: getListIndex,
  postListclassify: postListclassify,
  getShowDetail: getShowDetail,
  postScDetail: postScDetail,
  postQxDetail: postQxDetail,
  postAddflow:postAddflow,
  postLoginIm: postLoginIm,
  postDxyz: postDxyz,
  postDoreg: postDoreg,
  postListflow: postListflow,
  postChangecheck: postChangecheck,
  postChangenums: postChangenums,
  postDelcart: postDelcart,
  postCartorder: postCartorder,
  postDetalisconfirm: postDetalisconfirm,
  postCenterindex: postCenterindex,
  postCollectlist: postCollectlist,
  postColletdel: postColletdel,
  postListaddress: postListaddress,
  postAddaddr: postAddaddr,
  postAddrupdate: postAddrupdate,
  postVipadd: postVipadd,
  postListset: postListset,
  postUpdateset: postUpdateset,
  postOrderlist: postOrderlist,
  postToremind: postToremind,
  postLogisticsto: postLogisticsto,
  postDelorder: postDelorder,
  postOrderconfirm: postOrderconfirm,
  postOrderdetail: postOrderdetail,
  postTovoucher: postTovoucher,
  postGetvoucher: postGetvoucher,
  postToreceive: postToreceive,
  postTofract: postTofract,
  postFractshow: postFractshow,
  postFractdetail: postFractdetail,
  postFractrecord: postFractrecord,
  postFractfinish: postFractfinish,
  postCheckscore: postCheckscore,
  postFractconfirm: postFractconfirm,
  postAddrmoren: postAddrmoren,
  postAddrdel: postAddrdel,
  postPaywx: postPaywx,
  postUsevoucher: postUsevoucher,
  postVipagain: postVipagain,
  postVipqrcode: postVipqrcode,
  postAddorder: postAddorder,
  postOrderrefund: postOrderrefund,
  postFractcreate: postFractcreate,
  postEvalist: postEvalist,
  postEvaadd: postEvaadd,
  postDoadd: postDoadd,
  postnoticedetail: postnoticedetail,
};